<?php

namespace App\Http\Controllers\Agent;

use App\Models\Admin\TnetReginfo;
use App\Http\Controllers\Controller;
//实例化request
use Illuminate\Http\Request;
//use App\Agent;
//引入db，连接数据库
use Illuminate\Support\Facades\DB;

class ApplyController extends CommonController {


	/**
	 * 查看用户领取任务列表
	 */
	public function applyList(Request $request){

		//搜索键值
		$key=$request->input('key');
		$name=$request->input('name');
		$status=$request->input('status');
		$applyList=DB::table('tvm_apply')
			->leftJoin('tvm_mobile_detail', 'tvm_apply.mobile_id', '=', 'tvm_mobile_detail.id')
			->leftJoin('tnet_reginfo', 'tvm_apply.node_id', '=', 'tnet_reginfo.nodeid')
			->where('tvm_apply.agent_id','=',$_SESSION('agent_id'))
			->where('tvm_apply.status','>',0)
			->select('tvm_apply.*',
				'tnet_reginfo.nodecode',
				'tvm_mobile_detail.title')
			->where(function($query) use($key){
				if(!empty($key)){
					$query->where('tnet_reginfo.nodecode','like','%'.$key.'%');
				}
			})
			->where(function($query) use($name){
				if(!empty($name)){
					$query->where('tvm_apply.realname','like','%'.$name.'%');
				}
			})
			->where(function($query) use($status){
				if(!empty($status)){
					$status=$status-1;
					$query->where('tvm_apply.status','like','%'.$status.'%');
				}
			})
			->orderBy('tvm_apply.id', 'desc')
			->paginate(20);

		//追加每个账户推荐的vip数目
		foreach ($applyList as $k=>$v) {
			$node_id=$v->node_id;
			//查看用户推荐的vip数目
			$vipCount=DB::select("select count(*) as num from tnet_reginfo where introducer = ".$node_id." and nodeid in (select node_id from tvip_cost_order where status = 1)");
			$applyList[$k]->vipnum=$vipCount[0]->num;
			//查看用户推荐的真实vip数目
			$vipCount=DB::select("select count(*) as num from tnet_reginfo where introducer = ".$node_id." and nodeid in (select node_id from tvip_cost_order where status = 1 and cost_pv>0)");
			$applyList[$k]->realvipnum=$vipCount[0]->num;
		}
		//dd($applyList);
		return view('Agent.apply.list',['applyList'=>$applyList]);
	}



	/**
	 * 用户领取任务申请详情查看及编辑
	 */
	public function applyDetail(Request $request){
		$id=$request->input('id');
		$applyInfo=DB::table('TVM_APPLY')
			->leftJoin('tvm_mobile_detail', 'tvm_apply.mobile_id', '=', 'tvm_mobile_detail.id')
			->leftJoin('tnet_reginfo', 'tvm_apply.node_id', '=', 'tnet_reginfo.nodeid')
			->where('tvm_apply.id','=',$id)
			->select('tvm_apply.*',
				'tnet_reginfo.nodecode',
				'tvm_mobile_detail.title')
			->first();
		$node_id=$applyInfo->node_id;
		//查看用户推荐的vip数目
		$vipInfo=DB::select("select nodecode from tnet_reginfo where introducer = ".$node_id." and nodeid in (select node_id from tvip_cost_order where status = 1)");
		$vip['list']=$vipInfo;
		$vip['num']=count($vipInfo);

		$vipRealInfo=DB::select("select nodecode from tnet_reginfo where introducer = ".$node_id." and nodeid in (select node_id from tvip_cost_order where status = 1 and cost_pv>0)");
		$vip['reallist']=$vipRealInfo;
		$vip['realnum']=count($vipRealInfo);
		if($request->isMethod('post')){
			//编辑
			if($realname=$request->input('realname')){
				$data['realname']=$realname;
			}
			if($id_card=$request->input('id_card')){
				$data['id_card']=$id_card;
			}
			if($credit_card=$request->input('credit_card')){
				$data['credit_card']=$credit_card;
			}
			$res = DB::table('tvm_apply')->where('id',$id)->update($data);
			if($res){
				return ['code'=>200,'message'=>'申请资料修改成功','url'=>route('applylist')];
			}else{
				return ['code'=>300,'message'=>'申请资料修改失败'];
			}

		}
		return view('Agent.apply.detail',['applyInfo'=>$applyInfo,'vip'=>$vip]);
	}

	/**
	 * 用户直销套餐详情查看
	 */
	public function vipOrderDetail(Request $request){
		$node_id=$request->input('node_id');
		$nodecode=$request->input('nodecode');
		#查看用户推荐的vip数目
		$vipInfo=DB::select("select nodeid from tnet_reginfo where introducer = ".$node_id." and nodeid in (select node_id from tvip_cost_order where status = 1)");
		foreach($vipInfo as $k=>$v){
			$arr[$k]=$v->nodeid;
		}
		#防止列表为空报错
		if(empty($arr)){
			$arr[0]=-10000;
		}
		$orderList=DB::table('tvip_cost_order')
			->leftJoin('tnet_reginfo', 'tvip_cost_order.node_id', '=', 'tnet_reginfo.nodeid')
			->where('tvip_cost_order.status','=',1)
			->whereIn('tvip_cost_order.node_id',$arr)
			->select('tnet_reginfo.nodecode','tvip_cost_order.amount','tvip_cost_order.cost_name')
			->paginate(4);

		return view('Agent.apply.odetail',['orderList'=>$orderList,'nodecode'=>$nodecode],compact('orderList','node_id'),compact('orderList','nodecode'));
	}

	/**
	 * 用户申请进度
	 */
	public function scheduleDetail(Request $request){
		$mobile_id=$request->input('mobile_id');
		$node_id=$request->input('node_id');
		#走接口获取数据流
		$data['_cmd']='sendmobile_step';
		$data['ticket']=$this->get_ticket($node_id);
		#获取用户申请进度详情
		$res=$this->api_post($data);
		#获取手机详情
		$mobileInfo=DB::table('tvm_mobile_detail')
			->where('id','=',$mobile_id)
			->first();

		return view('Agent.apply.sdetail',['list'=>$res['data'],'mobileInfo'=>$mobileInfo]);
	}


	/**
	 * 用户任务完成明细
	 */
	public function taskDetail(Request $request){
		$node_id=$request->input('node_id');
		#走接口获取数据流
		$data['_cmd']='sendmobile_task_list';
		$data['ticket']=$this->get_ticket($node_id);
		#获取任务明细
		$res=$this->api_post($data);
		return view('Agent.apply.tdetail',['list'=>$res['data']]);
	}

	/**
	 * 删除用户领取申请
	 */
	public function applyDel(Request $request){
		$apply_id=$request->input('apply_id');
		$res=DB::table('tvm_apply')
			->where('id','=',$apply_id)
			->update(['status'=>0]);
		if($res){
			return ['code'=>200,'message'=>'删除成功'];
		}else{
			return ['code'=>300,'message'=>'删除失败'];
		}
	}


}